<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-body">
                <nav class="nav">
                    <ul class="nav nav-tabs d-flex align-items-center" id="app_tabs">
                        <li class="nav-item">
                            <a class="nav-link d-flex <#if (tabId == 1)>active</#if>" href="${request.contextPath}/manage/instance/opsList?tabId=1">配置检测</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link d-flex <#if (tabId == 2)>active</#if>" href="${request.contextPath}/manage/instance/opsList?tabId=2">命令检测</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link d-flex <#if (tabId == 3)>active</#if>" href="${request.contextPath}/manage/instance/opsList?tabId=3">重启记录</a>
                        </li>
                    </ul>
                </nav>

                <div class="btn-group" style="float:right">
                    <form class="form-inline" role="form">
                        <input name="tabId" id="tabId" value="${tabId!}" type="hidden"/>
                    </form>
                </div>


                <div class="card-group" id="configIndex">
                    <form class="row align-items-center" role="form">
                        <div class="input-group my-2">
                            <label class="col-form-label col-auto">
                                redis版本:
                            </label>
                            <div class="col-md-2">
                                <select name="versionId" id="versionId" class="form-select select2_category">
                                    <option value="" selected>请选择</option>
                                    <option value="0">所有</option>
                                    <#list redisVersionList! as redisVersion>
                                        <option value="${redisVersion.id!}">
                                            ${redisVersion.name!}
                                        </option>
                                    </#list>
                                </select>
                            </div>
                        </div>
                        <div class="input-group">
                            <div class="row">
                                <!-- 控件开始 -->
                                <label class="col-form-label col-auto">
                                    配置项:
                                </label>
                                <div class="col-md-2">
                                    <input type="text" name="configName" id="configName"
                                           class="form-control" />
                                </div>
                                <label class="col-form-label col-auto">
                                    比较:
                                </label>
                                <div class="col-auto">
                                    <select name="compareType" id="compareType" class="form-select select2_category">
                                        <#list compareTypeList! as compareTypeEnum>
                                            <option value="${compareTypeEnum.type!}">
                                                ${compareTypeEnum.info!}
                                            </option>
                                        </#list>
                                    </select>
                                </div>
                                <label class="col-form-label col-auto">
                                    比较值:
                                </label>
                                <div class="col-md-2">
                                    <input type="text" name="expectValue" id="expectValue"
                                           class="form-control" />
                                </div>
                            </div>
                            <div class="col-form-label col-auto" >
                                <button type="button" id="moduleBtn" class="btn btn-success" onclick="configCheck()">Ok</button>
                            </div>
                        </div>
                    </form>

                    <div class="table-responsive">
                        <table class="table table-striped table-bordered table-hover" name="checkResultList">
                            <thead>
                            <tr>
                                <th>redis版本</th>
                                <th>操作人</th>
                                <th>检测时间</th>
                                <th>检测条件</th>
                                <th>是否异常</th>
                                <th>异常查看</th>
                            </tr>
                            </thead>
                            <tbody>
                            <#list checkResultList! as checkResult>
                                <tr>
                                    <td>
                                        <#list redisVersionList as redisVersion>
                                            <#if (redisVersion.id==checkResult.versionId)>
                                                ${redisVersion.name!}
                                            </#if>
                                        </#list>
                                        <#if !(checkResult.versionId??) || (checkResult.versionId == 0)>
                                            所有
                                        </#if>
                                    </td>
                                    <td>
                                        ${checkResult.userName!}
                                    </td>
                                    <td>
                                        ${checkResult.createTimeStr!}
                                    </td>
                                    <td>
                                        ${checkResult.configName!}
                                        &nbsp;

                                        <#list compareTypeList as compareTypeEnum>
                                            <#if (checkResult.compareType == compareTypeEnum.type)>
                                                ${compareTypeEnum.info!}
                                            </#if>
                                        </#list>
                                        &nbsp;
                                        ${checkResult.expectValue!}
                                    </td>
                                    <td>
                                        <#if (checkResult.success==true)>否</#if>
                                        <#if (checkResult.success==false)>是</#if>
                                    </td>

                                    <td>
                                        <#if (checkResult.success==true)></#if>
                                        <#if (checkResult.success==false)>
                                            <button class="btn btn-warning btn-sm" style="float: right;">
                                                <a href="${request.contextPath}/manage/instance/getConfigCheck?uuid=${checkResult.key!}"
                                                   target="_blank"><font style="color: white">查看修复</font></a>
                                            </button>
                                        </#if>
                                    </td>
                                </tr>
                            </#list>
                            </tbody>
                        </table>
                    </div>
                </div>

                <div class="card-group" id="commandIndex" style="min-height: 80%">
                    <form class="row align-items-center bordered">
                        <div class="modal-contained">
                            <div class="row my-2">
                                <label class="col-form-label col-auto">
                                    宿主机ip:
                                </label>
                                <div class="col-md-5">
                                    <select id="machineIp" name="machineIp"
                                            class="selectpicker show-tick border rounded"
                                            data-live-search="true" title="选择宿主机"
                                            data-size="8">
                                    </select>
                                </div>
                                <label class="col-form-label col-auto">
                                    pod ip:
                                </label>
                                <div class="col-md-4">
                                    <select id="podIp" name="podIp"
                                            class="selectpicker show-tick border rounded"
                                            data-live-search="true" title="选择pod"
                                            data-size="8">
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="modal-contained">
                            <div class="row mb-2">
                                <label class="col-form-label col-auto">
                                    命令:
                                </label>
                                <div class="col-auto">
                                    <select name="command" id="command" class="form-select select2_category">
                                        <option value="bgsave">bgsave</option>
                                        <option value="bgrewriteaof">bgrewriteaof</option>
                                    </select>
                                </div>
                                <button type="button" id="commandCheckBtn" class="btn btn-success offset-md-6 col-auto" onclick="commandCheck()">Ok</button>
                            </div>
                        </div>
                    </form>
                    <div class="table-responsive">
                        <table class="table table-striped table-bordered table-hover" name="tableDataList">
                            <thead>
                            <tr>
                                <th>操作人</th>
                                <th>检测时间</th>
                                <th>宿主机ip</th>
                                <th>pod ip</th>
                                <th>执行命令</th>
                                <th>是否失败</th>
                                <th>失败详情</th>
                            </tr>
                            </thead>
                            <tbody>
                            <#list commandCheckResult! as checkResult>
                                <tr>
                                    <td>${checkResult.userName!}</td>
                                    <td>${checkResult.createTimeStr!}</td>
                                    <td>${checkResult.machineIps!}</td>
                                    <td>${checkResult.podIp!}</td>
                                    <td>${checkResult.command!}</td>
                                    <td>
                                        <#if (checkResult.success==true)>否</#if>
                                        <#if (checkResult.success==false)>是</#if>
                                    </td>
                                    <td>
                                        <#if (checkResult.success==true)></#if>
                                        <#if (checkResult.success==false)>
                                            <button class="btn btn-warning btn-sm" style="float: right;">
                                                <a href="${request.contextPath}/manage/instance/getCommandCheck?uuid=${checkResult.key!}"
                                                   target="_blank"><font style="color: white">查看</font></a>
                                            </button>
                                        </#if>
                                    </td>
                                </tr>
                            </#list>
                            </tbody>
                        </table>
                    </div>
                </div>


                <div class="portlet box light-grey" id="restartIndex">
                    <div class="card-header">
                        <div class="card-title">
                            <i class="bi bi-globe"></i>应用滚动重启/修改配置记录
                        </div>
                    </div>

                    <div class="row my-2">
                        <form class="row d-flex justify-content-end" role="form" action="${request.contextPath}/manage/instance/opsList" method="get" id="appList">
                            <div class="col-auto">
                                <input type="text" class="form-control" id="appId" name="appId" value="${appId!}"
                                       placeholder="应用ID">
                                <input type="hidden" name="pageNo" id="pageNo">
                                <input type="hidden" name="tabId" value="3">
                            </div>
                            <button type="submit" class="btn btn-info col-auto">查询</button>
                        </form>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-striped table-bordered table-hover table-sm" id="tableDataList">
                            <thead>
                            <tr>
                                <th>记录ID</th>
                                <th>操作人</th>
                                <th>应用ID</th>
                                <th>应用名称</th>
                                <th>涉及实例<br><font size="1" style="font-weight: bold">点击实例查看日志</font></th>
                                <th>重启类型</th>
                                <th>日志</th>
                                <th>开始时间</th>
                                <th>结束时间</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <#list restartRecordList! as configRestartRecord>

                                <#if configRestartRecord.status?? && (configRestartRecord.status == 3)>
                                    <tr style="color:red">
                                </#if>
                                <#if configRestartRecord.status?? && (configRestartRecord.status == 4)>
                                    <tr style="color:dodgerblue">
                                </#if>
                                <#if configRestartRecord.status?? && ((configRestartRecord.status == 1) || (configRestartRecord.status == 5))>
                                    <tr style="color:#e5680f">
                                </#if>
                                <#if configRestartRecord.status?? && (configRestartRecord.status == 6)>
                                    <tr style="color:#c3680f">
                                </#if>
                                <#if !(configRestartRecord.status??) ||  (
                                        (configRestartRecord.status != 3) && (configRestartRecord.status != 4)
                                        && (configRestartRecord.status != 1) && (configRestartRecord.status != 5)
                                        && (configRestartRecord.status != 6)
                                )>
                                    <tr>
                                </#if>
                                <td>
                                    ${configRestartRecord.id!}
                                </td>
                                <td>
                                    ${configRestartRecord.userName!}
                                </td>
                                <td>
                                    ${configRestartRecord.appId!}
                                </td>
                                <td>
                                    ${configRestartRecord.appName!}
                                </td>
                                <td>
                                    <#list configRestartRecord.instanceIdList as instance_id>
                                        <#if instanceInfoMap?? && instanceInfoMap?api.get(instance_id)??>
                                            <#assign hostPort = instanceInfoMap?api.get(instance_id).hostPort!>
                                        <#else>
                                            <#assign hostPort = ''>
                                        </#if>
                                        <a href="${request.contextPath}/manage/instance/log?instanceId=${instance_id!}" target="_blank">${instance_id!}</a>(${hostPort!})
                                        <br/>
                                    </#list>
                                </td>
                                <td>
                                    <#if configRestartRecord.operateType?? && (configRestartRecord.operateType == 0)>
                                        滚动重启
                                    </#if>
                                    <#if configRestartRecord.operateType?? && (configRestartRecord.operateType == 1)>
                                        修改冷配置并重启
                                    </#if>
                                    <#if configRestartRecord.operateType?? && (configRestartRecord.operateType == 2)>
                                        修改热配置
                                    </#if>
                                    <#if configRestartRecord.operateType?? && (configRestartRecord.operateType == 3)>
                                        升级模块
                                    </#if>
                                </td>
                                <td escapeXml="false">
                                    <#list configRestartRecord.logList as log>
                                        ${log!}
                                        <br/>
                                    </#list>
                                </td>
                                <td>
                                    ${configRestartRecord.startTime!?string("yyyy-MM-dd HH:mm:ss")}
                                </td>
                                <td>
                                    ${configRestartRecord.endTime!?string("yyyy-MM-dd HH:mm:ss")}
                                </td>
                                <td>
                                    <#if configRestartRecord.status?? && (configRestartRecord.status == 0)>
                                        等待中
                                    </#if>
                                    <#if configRestartRecord.status?? && (configRestartRecord.status == 1)>
                                        运行中
                                    </#if>
                                    <#if configRestartRecord.status?? && (configRestartRecord.status == 2)>
                                        成功
                                    </#if>
                                    <#if configRestartRecord.status?? && (configRestartRecord.status == 3)>
                                        失败
                                    </#if>
                                    <#if configRestartRecord.status?? && (configRestartRecord.status == 4)>
                                        配置已改待重启
                                    </#if>
                                    <#if configRestartRecord.status?? && (configRestartRecord.status == 5)>
                                        配置已改重启中
                                    </#if>
                                    <#if configRestartRecord.status?? && (configRestartRecord.status == 6)>
                                        已停止
                                    </#if>
                                </td>
                                <td>
                                    <#if configRestartRecord.status?? && (((configRestartRecord.status == 1) && (configRestartRecord.operateType == 0)) || ((configRestartRecord.status == 5) && (configRestartRecord.operateType == 1)))>
                                        <button class="btn btn-info" onclick="stopRestart('${configRestartRecord.appId!}')">停止</button>
                                    </#if>
                                </td>
                                </tr>
                            </#list>
                            </tbody>
                        </table>
                        <div style="margin-top: 10px;">
                            <span>
                              <div id="pageDetail" style="float:left;padding-top:7px;color:#4A64A4;">
                                <#if page??>
                                    共${page.totalPages!}页,${page.totalCount!}条
                                </#if>
                              </div>
                              <nav id='ccPagenitor' aria-label="Page navigation example" class="d-inline-flex float-end">
                              </nav>
                            </span>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        var searchDate = $('#searchDate').val();
        if (searchDate == null || searchDate == '') {
            var time = new Date();
            var day = ("0" + time.getDate()).slice(-2);
            var month = ("0" + (time.getMonth() + 1)).slice(-2);
            var today = time.getFullYear() + "-" + (month) + "-" + (day);
            $('#searchDate').val(today);
        }
        var tabVal = $('#tabId').val();
        if (tabVal == 1) {
            $('#configIndex').removeAttr("hidden");
            $('#commandIndex').attr("hidden", "hidden");
            $('#restartIndex').attr("hidden", "hidden");
            $('#aofMonitorIndex').attr("hidden", "hidden");
            $('#backupIndex').attr("hidden", "hidden");
        } else if (tabVal == 2) {
            $('#configIndex').attr("hidden", "hidden");
            $('#commandIndex').removeAttr("hidden");
            $('#restartIndex').attr("hidden", "hidden");
            $('#aofMonitorIndex').attr("hidden", "hidden");
            $('#backupIndex').attr("hidden", "hidden");
        }else if (tabVal == 3) {
            $('#configIndex').attr("hidden", "hidden");
            $('#commandIndex').attr("hidden", "hidden");
            $('#restartIndex').removeAttr("hidden");
            $('#aofMonitorIndex').attr("hidden", "hidden");
            $('#backupIndex').attr("hidden", "hidden");
        }else if (tabVal == 4) {
            $('#configIndex').attr("hidden", "hidden");
            $('#commandIndex').attr("hidden", "hidden");
            $('#restartIndex').attr("hidden", "hidden");
            $('#aofMonitorIndex').removeAttr("hidden");
            $('#backupIndex').attr("hidden", "hidden");
        } else if (tabVal == 5) {
            $('#configIndex').attr("hidden", "hidden");
            $('#commandIndex').attr("hidden", "hidden");
            $('#restartIndex').attr("hidden", "hidden");
            $('#aofMonitorIndex').attr("hidden", "hidden");
            $('#backupIndex').removeAttr("hidden");
        }

        {
            $.post('${request.contextPath}/manage/instance/getMachineList.json',
                {
                    ip: null,
                    realIp: null,
                    searchType: 1
                },
                function (data) {
                    var status = data.status;
                    if (status == 1) {
                        var ipSet = data.ipSet;
                        $('#machineIp').append("<option value=''>" + '请选择' + "</option>");
                        for (var key in ipSet) {
                            $('#machineIp').append("<option value='" + ipSet[key] + "'>" + ipSet[key] + "</option>");
                        }
                        $('#machineIp').selectpicker('refresh');
                        $('#machineIp').selectpicker('render');

                        $('.dropdown-toggle').on('click', function () {
                            $('.dropdown-toggle').dropdown();
                        });
                    }
                }
            );

            $.post('${request.contextPath}/manage/instance/getMachineList.json',
                {
                    ip: null,
                    realIp: null,
                    searchType: 2
                },
                function (data) {
                    var status = data.status;
                    if (status == 1) {
                        var ipSet = data.ipSet;
                        $('#podIp').append("<option value=''>" + '请选择' + "</option>");
                        for (var key in ipSet) {
                            $('#podIp').append("<option value='" + ipSet[key] + "'>" + ipSet[key] + "</option>");
                        }
                        $('#podIp').selectpicker('refresh');
                        $('#podIp').selectpicker('render');

                        $('.dropdown-toggle').on('click', function () {
                            $('.dropdown-toggle').dropdown();
                        });
                    }
                }
            );
        }
    });

    //验证是数字
    function testisNum(id) {
        var value = document.getElementById(id).value;
        if (value != "" && isNaN(value)) {
            alert("请输入数字类型!");
            document.getElementById(id).value = "";
            document.getElementById(id).focus();
        }
    }

    function sendExpAppsStatDataEmail() {
        var searchDate = document.getElementById("searchDate").value;
        $.get('${request.contextPath}/manage/app/tool/sendExpAppsStatDataEmail', {searchDate: searchDate});
        alert("异常应用日报已发送，请查收")
    }

    //配置检测
    function configCheck(){
        var versionId = document.getElementById("versionId");
        var configName = document.getElementById("configName");
        if (configName.value == ""){
            alert("请填写配置名");
            configName.focus();
            return false;
        }
        var expectValue = document.getElementById("expectValue");
        var compareType = document.getElementById("compareType");
        document.getElementById("moduleBtn").disabled = "true";
        alert("即将执行检测，请稍等。")
        $.post(
            '${request.contextPath}/manage/instance/configCheck.json',
            {
                versionId: versionId.value,
                configName: configName.value,
                compareType: compareType.value,
                expectValue: expectValue.value
            },
            function (data) {
                document.getElementById("moduleBtn").disabled = false;
                var status = data.status;
                if (status == 1) {
                    alert("已执行，即将刷新页面展示结果");
                    window.location.reload();
                } else {
                    alert(data.message);
                    window.location.reload();
                }
            }
        );
    }

    //命令检测
    function commandCheck(){
        var machineIp = document.getElementById("machineIp");
        var podIp = document.getElementById("podIp");
        var command = document.getElementById("command");
        document.getElementById("commandCheckBtn").disabled = "true";
        if (confirm("确认执行命令检测？")) {
            alert("即将执行，请稍等。");
            $.post(
                '${request.contextPath}/manage/instance/commandCheck.json',
                {
                    machineIps: machineIp.value,
                    podIp: podIp.value,
                    command: command.value
                },
                function (data) {
                    document.getElementById("commandCheckBtn").disabled = false;
                    var status = data.status;
                    if (status == 1) {
                        alert("已执行，即将刷新页面展示结果");
                        window.location.reload();
                    } else {
                        alert(data.message);
                        window.location.reload();
                    }
                }
            );
        }
    }

    /**
     * 停止滚动重启
     * @param appId
     */
    function stopRestart(appId) {
        if (confirm("确认停止重启任务吗？")) {
            $.get(
                '${request.contextPath}/manage/app/restart/stopRestart?appId=' + appId,
                function (data) {
                    var status = data.status;
                    if (status == 200) {
                        alert(data.data);
                    } else {
                        alert(data.error);
                    }
                }
            );
        }
    }

</script>
<#include "page.html">

